<?php

namespace StudyBuddy\Forms;

use \StudyBuddy\Validate;
use \StudyBuddy\Request;

class Uploadresourceform extends Form {
    const CAPTCHA_ERROR = 'Incorrect image verification text<br/>Please try again';

    /**
     * Name of form template file
     * The name of actual template should be
     * set in sub-class
     *
     * @var string
     */
    protected $template = 'tplUploadresourceform';

    protected function doValidate() {
        $this->validateResource();
    }

    protected function init() {
        
    }

    /**
     * If form hasUploads and has uploaded file 'resource'
     * then: check that if does not have 'error' code
     * theck that the 'size' > 0 and 'tmp_name' !== 'none' and not empty
     * check that size < (MAX_AVATAR_FILE_SIZE in setting)
     * If any of this pre-checks fail then delete the uploaded file
     * and set the form error
     *
     * @return object $this
     */
    protected function validateResource() {
        d('cp');
        if ($this->hasUploads() && !empty($this->aUploads['resource_file'])) {
            $a = $this->aUploads['resource_file'];

            if (!is_array($a) || (0 == $a['size'] && empty($a['name']))) {
                d('resource was not uploaded');

                return $this;
            }

            d('cp');

            /**
             * If bad error code
             */
            if (UPLOAD_ERR_OK !== $errCode = $a['error']) {
                e('Upload of avatar failed with error code ' . $a['error']);
                if (UPLOAD_ERR_FORM_SIZE === $errCode) {
                    $this->setError('resource_file', 'Uploaded file exceeds maximum allowed size');
                    return $this;
                } elseif (UPLOAD_ERR_INI_SIZE === $errCode) {
                    $this->setError('resource_file', 'Uploaded file exceeds maximum upload size');
                    return $this;
                } else {
                    $this->setError('resource_file', 'There was an error uploading the resource file');
                    return $this;
                }
            } else {

                $maxSize = $this->oRegistry->Ini->MAX_AVATAR_UPLOAD_SIZE;
                d('$maxSize ' . $maxSize);

                /**
                 * Check If NOT allow file type
                 */
                if (!empty($a['type'])) {
                    if (
                            'text/plain' === $a['type'] ||
                            'application/msword' === $a['type'] ||
                            'image/tiff' === $a['type'] ||
                            'image/x-tiff' === $a['type'] ||
                            'application/mspowerpoint' === $a['type'] ||
                            'application/powerpoint' === $a['type'] ||
                            'application/vnd.ms-powerpoint' === $a['type'] ||
                            'application/x-mspowerpoint' === $a['type'] ||
                            'application/pdf' === $a['type']
                    ) {
                        // allow file type
                    } else {
                        $this->setError('profile_file', 'Uploaded file was not allow file type');
                        return $this;
                    }
                }

                /**
                 * If file too large
                 */
                if (!empty($a['tmp_name'])) {
                    if (false === $size = @\filesize($a['tmp_name'])) {
                        $this->setError('resource_file', 'There was an error uploading the resource file');
                        return $this;
                    }

                    d('size: ' . $size);

                    if (($size / $maxSize) > 1.1) {
                        d('$size / $maxSize: ' . $size / $maxSize);
                        $this->setError('resource_file', 'File too large. It must be under ' . ($maxSize / 1024000) . 'MB');
                    }
                }
            }
        }

        return $this;
    }

}
